<?php
/*

Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
/**
	list_fields.php
	1. just list currently available fields
*/

	// Define that we are returning XML content & not to cache:
	header('Content-Type: text/xml');
	header('Cache-control: no-cache');

	$module_name = $_GET['module_name'];
	$table_id = $_GET['table_id'];
	
	$project_home = $_GET['project_home'];
	
	$SEMF_ROOT = '../../';

$module_xml = $SEMF_ROOT.$project_home.$module_name.'/module.xml';



if (file_exists($module_xml)) {
	$doc = new DOMDocument();
	$doc->load($module_xml);
	
	$table_elements = $doc-> getElementsByTagName('table');
	
	
	$table_element = $table_elements -> item($table_id);

	//$field_list = $target_table-> childNodes;
	
		$is_interface = $table_element->getAttribute("interface");
		if($is_interface == "yes")
		{
			//some code to handle interface -- anyway this shouldn't be displayed
			echo 'interface';
			exit;
		}
		$inherited_elements = $table_element->childNodes;
		$all_fields = array();
		
		foreach ($inherited_elements as $inherited_element)
		{
			if($inherited_element->tagName != "inherited")
			{
				continue;
			}
			$inherited_table = $inherited_element->nodeValue;
			$pos = strpos($inherited_table, ".");
			$inherited_table_owner_module = substr($inherited_table, 0, $pos);
			$inherited_table_name = trim(substr($inherited_table, $pos+1));
			
			
			$inherited_module_xml = $SEMF_ROOT.$project_home.trim($inherited_table_owner_module)."/module.xml";

			$inherited_doc = new DOMDocument();
			$inherited_doc->load($inherited_module_xml);
			
			$inherited_table_elements = $inherited_doc->documentElement->childNodes;
			foreach ($inherited_table_elements as $inherited_table_element)
			{
				if($inherited_table_element->nodeType != XML_ELEMENT_NODE)
				{
					continue;
				}
				if($inherited_table_element->getAttribute("name") == $inherited_table_name)
				{
					$inherited_field_elements = $inherited_table_element->childNodes;
					
					foreach($inherited_field_elements as $inherited_field_element)
					{			
						if($inherited_field_element->tagName != "field")
						{
							continue;
						}
						$field_info = array(
								"name"=> $inherited_field_element->getAttribute("name"),
								"type"=> $inherited_field_element->getAttribute("type"),
								"isarray"=> $inherited_field_element->getAttribute("isarray"),
								"inherited"=>true);
						
						$all_fields[count($all_fields)] = $field_info;
					}
					break;
				}
			}
		}
		
		$field_elements = $table_element->childNodes;
		$i=0;
		foreach($field_elements as $field_element)
		{		
			if($field_element->tagName != "field")
			{
				$i++;
				continue;
			}
			$field_info = array(
					"name"=> $field_element->getAttribute("name"),
					"type"=> $field_element->getAttribute("type"),
					"isarray"=> $field_element->getAttribute("isarray"),
					"inherited"=>false,
					"index"=>$i);
			$i++;
			$all_fields[count($all_fields)] = $field_info;		
		}
		
		//print_r($all_fields);

?>
<style type="text/css">
<!--
.style_table {
	color: #003399;
	font-weight: bold;
	font-family: Geneva, Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-style: italic;
}
-->
</style>
<div style="padding:20px ;">
<div style="padding:5px;">
<img src="../lib/images/back.png" onclick="javascript:getModuleSchemaData('<?php echo $module_name?>', '<?php echo $project_home ?>');" title="Go back to Entity view";"  />
</div>
<div style="padding:5px;">
<img width="71" height="25" src="../lib/images/addattributelightblue.png" onmouseover="javascript:changeImage(this,'addattributeblack.png');" onmouseout="javascript:changeImage(this,'addattributelightblue.png');" onmousedown="javascript:changeImage(this,'addattributegreen.png');"  onclick="javascript:addFieldMode('<?php echo $table_id ?>','<?php echo $module_name ?>','<?php echo $project_home ?>');"/>
</div>

<table id="field_table" width="486" cellpadding="0" cellspacing="2" bgcolor="#000066" style="margin:10px">
<?php
	
	foreach ( $all_fields as $field )
	{
		
			$field_name = $field['name'];
			$field_type = $field['type'];
			$is_array = $field['isarray'];
			$multiplicity=($is_array == 'yes')?'(M)':'(S)';
			if(isset($field['index']))
			{
				$field_id = $field['index'];
			}
			//echo $field_name.' '.$field_type.'<br>';
			//$color_code=($field_id%2==0)?"#C9D6F5":"#DEE2FE";


			$output_string='<tr  height="25" class="style_table" style="background-image:url(../lib/images/menuidle2.png);" onmouseover="javacript:setBg(this,\'activemenu.png\');"  onmouseout="javascript:setBg(this,\'menuidle2.png\');"  id="online_edit'.$field_id.'" width="400"> ';
			
			
			
			if(!$field['inherited']){
				$output_string = $output_string.'<td width="25%" >'.$field_name.'</td><td width="5%">'.$multiplicity.'</td><td width="30%" >'.$field_type.'</td><td width="20%"><img width="71" height="25" src="../lib/images/editlightblue.png" onmouseover="javascript:changeImage(this,\'editblue.png\');" onmouseout="javascript:changeImage(this,\'editlightblue.png\');" onmousedown="javascript:changeImage(this,\'editgreen.png\');" onclick="javascript:editFieldMode(\''.$multiplicity.'\',\''.$field_name.'\',\''.$field_type.'\',\''.$field_id.'\',\''.$table_id.'\',\''.$module_name.'\',\''.$project_home.'\');"/></td>
			<td width="20%"  class="style_table" ><img width="71" height="25" src="../lib/images/deletelightblue.png" onmouseover="javascript:changeImage(this,\'deletered.png\');" onmouseout="javascript:changeImage(this,\'deletelightblue.png\');" onmousedown="javascript:changeImage(this,\'deletegreen.png\');" onclick="javascript:deleteField(\''.$field_id.'\',\''.$table_id.'\',\''.$module_name.'\',\''.$project_home.'\');" /></td>
			</tr>';
			}else {
				$output_string = $output_string.'<td width="35%" style="color:#666666;">'.$field_name.'</td><td width="5%" style="color:#666666;">'.$multiplicity.'</td><td width="30%" style="color:#666666;">'.$field_type.'</td><td ><img src="../lib/images/editsilver.png" /></td><td><img src="../lib/images/deletesilver.png" /></td></tr>';
			}
			echo $output_string;
			
			
			if(!($field['inherited'])){
				$field_id ++;
			}
	//include('new_field.php');

	}
}

?>


</table>



<?php
if( count($all_fields) == 0 )
{
	echo '<i> There are no fields. To add a new field<b> Click</b> add new</i>';
}
?>
</div>
